package com.yqz.sys.controller;

import com.yqz.common.CodeMsg;
import com.yqz.common.Result;
import com.yqz.common.base.controller.BaseController;
import com.yqz.common.util.WebUtil;
import com.yqz.common.validator.ValidatorUtil;
import com.yqz.sys.domain.CompUser;
import com.yqz.sys.form.CompUserForm;
import com.yqz.sys.query.CompUserQuery;
import com.yqz.sys.service.CompUserService;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Enumeration;
import java.util.List;

@RestController
@RequestMapping("comp/user")
public class CompUserController extends BaseController<CompUserQuery, CompUserForm> {

    @Autowired
    private CompUserService userService;

    public CompUserController(CompUserService userService) {
        super(userService);
    }

    /**
     * 用户登陆
     * @param phone
     * @param password
     * @return
     */
    @PostMapping("login.do")
    public Object login(String phone,String password){
        return userService.login(phone,password);
    }

    /**
     * 更具用户id查询角色id
     * @param id
     * @return
     */
    @RequestMapping("userRoles.do")
    public Object userRoles(Integer id){
        return userService.queryUserRoles(id);
    }

    /**
     * 设置角色
     * @param uId
     * @param rId
     * @return
     */
    @RequestMapping("editRole.do")
    public Object editRole(@RequestParam("uId")Integer uId, @RequestParam("rId") List<Integer> rId){
        return userService.editUserRoles(uId,rId);
    }

    /**
     * 修改密码
     * @param password
     * @param newPassword
     * @return
     */
    @RequestMapping("updatePwd.do")
    public Object updatePwd(@RequestParam("password")String password, @RequestParam("newPassword")String newPassword){
        CompUser user = WebUtil.getCurrentCompUser();
        if(user == null){
            return new Result(CodeMsg.USER_LOGIN_NVALID_ERROR);
        }
        Result rs = userService.updatePwd(user,password,newPassword);
        if(rs.getCode().equals(CodeMsg.SUCCESS.getCODE())) {
            tologout(WebUtil.getRequest());
        }
        return rs;
    }

    /**
     * 清理Session
     * @param request
     * @return
     */
    @RequestMapping("tologout.do")
    public void tologout(HttpServletRequest request) {
        Enumeration em = request.getSession().getAttributeNames();
        while (em.hasMoreElements()) {
            request.getSession().removeAttribute(em.nextElement().toString());
        }
    }

    /**
     * 获取所有有效的会计用户
     * @return
     */
    @RequestMapping("getAccountings.do")
    public Object getAccountings(){
        return userService.getAccountings();
    }

    /**
     * 修改个人信息
     * @return
     */
    @RequestMapping("updateUserInfo.do")
    public Object updateUserInfo(CompUserForm form){
        form.setCompId(WebUtil.getCurrentCompUser().getCompId());
        form.setDeptId(WebUtil.getCurrentCompUser().getDeptId());
        form.setPassword(WebUtil.getCurrentCompUser().getPassword());
        ValidatorUtil.validate(form);
        return userService.update(form);
    }

}
